package com.example.guessthesoundtrack;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.view.MotionEvent;

public class Letter extends Sprite {
	private ArrayList<Shadow> shadows;
	
	private Shadow shadow;
	
	private Paint paint;
	private Rect bounds;
	
	private boolean key;
	private float dx, dy;
	private char letter;

	public Letter(ArrayList<Shadow> shadows, Bitmap clicked, Bitmap unclicked, float x, float y, float width, float height, boolean key, char letter) {
		super(clicked, unclicked, new Rect((int)x, (int)y, (int)(x + width), (int)(y + height)), x, y, width, height);
		this.shadows = shadows;
		this.letter = letter;

		this.bounds = new Rect();
		this.paint = new Paint();
		this.paint.setTextSize(48f);
		this.paint.setTextAlign(Align.CENTER);
		this.paint.setTypeface(Typeface.DEFAULT_BOLD);
		this.paint.getTextBounds("" + letter, 0, 1, bounds);
	}

	public Shadow getShadow() {
		return shadow;
	}

	public void setShadow(Shadow shadow) {
		this.shadow = shadow;
	}

	public boolean isKey() {
		return key;
	}

	public void setKey(boolean key) {
		this.key = key;
	}

	public float getDX() {
		return dx;
	}

	public void setDX(float dx) {
		this.dx = dx;
	}

	public float getDY() {
		return dy;
	}

	public void setDY(float dy) {
		this.dy = dy;
	}

	public char getLetter() {
		return letter;
	}

	public void setLetter(char letter) {
		this.letter = letter;
	}
	
	public void setPosition(float x, float y) {
		setX(x);
		setY(y);
		setRect(new Rect((int)x, (int)y, (int)(x + getWidth()), (int)(y + getHeight())));
	}

	@Override
	public void draw(Canvas canvas) {
		if (shadow == null) {
			canvas.drawBitmap((isDown() ? getClicked() : getUnclicked()), null, getRect(), null);
			canvas.drawText("" + letter, getX() + (getWidth() / 2f), getY() + (getHeight() / 2f) + (bounds.height() / 2f) + ((getHeight() / 16f) * (isDown() ? 1 : -1)), paint);
		}
	}

	@Override
	public void update(int delta) { }

	@Override
	public void onClick(MotionEvent event, float x, float y) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			setDown(true);
			break;
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_UP:
			setDown(false);
			if (shadow != null)
				return;
			
			boolean found = false;
			int i = 0;
			while (!found && i < shadows.size()) {
				if (shadows.get(i).getLetter() == null && !shadows.get(i).isSpace() && shadows.get(i).isColliding(x, y))
					found = true;
				else
					i++;
			}
			if (found)
				shadows.get(i).setLetter(this);
			break;
		}	
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean isColliding(float x, float y) {
		if (shadow != null)
			return false;
		return (x >= getX() && y >= getY() & x <= getX() + getWidth() && y <= getY() + getHeight());
	}
}